# -*- coding: utf-8 -*-
import MySQLdb
import MySQLdb.cursors
from xlwt import *
import xlrd
import sys
from xlutils.copy import copy
import os

reload(sys)
sys.setdefaultencoding('utf8')

"""
author：zhangqing
function: 数据初始化对比：cvs1.0与cvs2.0 投资机构列表对比（机构名称、成立时间、详细地址、联系电话、机构简介、机构网址、机构类型、
                                                        资本来源、管理资金总额、是否基金业协会备案、投资伦次、策略描述）
"""

def searchCvs1OrgList():
    # db = MySQLdb.connect("119.254.13.183", "amy_zhang", "ZHE0yeuWHAkqyUdw", "datacenter_online_20180609", charset='utf8',
    #                      cursorclass=MySQLdb.cursors.DictCursor)
    db = MySQLdb.connect("39.106.211.74", "read", "aaKcKA6iDXyJVeF6", "datacenter_online", charset='utf8',
                         cursorclass=MySQLdb.cursors.DictCursor)

    cursor = db.cursor()

    sql = "SELECT COUNT(a.id) a FROM dc_organization a WHERE a.`status`='1' ORDER BY a.id DESC"
    cursor.execute(sql)
    resultsCount = cursor.fetchone()
    pageCount = resultsCount["a"]/100

    for index in range(pageCount):
    # for index in range(115,386):
        limitStart = index * 100

        print("limitStart====================================================",limitStart)

        sql = "SELECT a.id AS orgIdOrCompanyId, a.`name` AS orgName, c.cn_short AS short_name, a.capital_from_type AS source, a.info_status, b.org_type AS orgType, " \
              "c.begin_time, c.reg_location AS address,'' AS telephone, c.cn_desc AS description, c.web_url AS website, a.management_money AS money, " \
              "'' AS strategyDescribe,ndrc_record_type AS fund_association, a.ext1 AS beizhu " \
              "FROM " \
              "dc_organization a " \
              "LEFT JOIN " \
              "( SELECT type.org_id, GROUP_CONCAT(type.org_type) AS org_type FROM dc_organization_type type GROUP BY type.org_id ) b " \
              "ON a.id = b.org_id " \
              "LEFT JOIN " \
              "dc_company c ON a.company_id = c.id " \
              "WHERE a.`status` = '1' " \
              "ORDER BY a.`name` DESC LIMIT %d,100 "%limitStart

        # sql = "SELECT a.id AS orgIdOrCompanyId, a.`name` AS orgName, c.cn_short AS short_name, a.capital_from_type AS source, a.info_status, b.org_type AS orgType, " \
        #       "c.begin_time, c.reg_location AS address,'' AS telephone, c.cn_desc AS description, c.web_url AS website, a.management_money AS money, " \
        #       "'' AS strategyDescribe,a.ext1 AS beizhu " \
        #       "FROM " \
        #       "datacenter_online_20180609.dc_organization a " \
        #       "LEFT JOIN " \
        #       "( SELECT type.org_id, GROUP_CONCAT(type.org_type) AS org_type FROM dc_organization_type type GROUP BY type.org_id ) b " \
        #       "ON a.id = b.org_id " \
        #       "LEFT JOIN " \
        #       "dc_company c ON a.company_id = c.id " \
        #       "WHERE a.`status` = '1' and a.`name` = '黔海文化旅游基金管理（深圳）有限公司'"


        cursor.execute(sql)
        orgResults1 = cursor.fetchall()
        fields = cursor.description
        compareCvs2OrgList(orgResults1, fields)

    db.close()

def compareCvs2OrgList(orgResults1, fields):
    db = MySQLdb.connect("rm-2ze9cx91355z4mk9hpo.mysql.rds.aliyuncs.com", "cvs2.0_read_only", "aaKcKA6iDXyJVeF6",
                         "cvs_prod", charset='utf8',cursorclass=MySQLdb.cursors.DictCursor)
    cursor = db.cursor()

    orgNameStr = ''
    for index in range(len(orgResults1)):
        print(index)
        orgName1 = orgResults1[index].get(u"orgName")
        if orgName1 != None:
            orgName1 = MySQLdb.escape_string(orgName1)
            orgNameStr += ",'"+orgName1+"'"
    print(orgNameStr)
    orgNameStr=orgNameStr[1:]

    viewOrgsql = "SELECT company_id AS orgIdOrCompanyId, full_name AS orgName, short_name, source, '' AS info_status, org_type_data AS orgType, " \
                 "begin_time, contact_address_data AS address, contact_data AS telephone, description, website, management_money AS money, " \
                 "strategy_describe AS strategyDescribe, fund_association, enable_search AS beizhu " \
                 "FROM view_organization W" \
                 "HERE full_name IN (%s) " \
                 "ORDER BY full_name DESC"%orgNameStr
    cursor.execute(viewOrgsql)
    viewOrgResults2 = cursor.fetchall()
    for index in range(len(orgResults1)):
        orgName1 = orgResults1[index].get(u"orgName")
        short_name1 = orgResults1[index].get(u"short_name")
        begin_time1 = orgResults1[index].get(u"begin_time")
        #1.0库中注册地址
        regAddress1 = orgResults1[index].get(u"address")
        description1 = orgResults1[index].get(u"description")
        website1 = orgResults1[index].get(u"website")
        orgType1 = orgResults1[index].get(u"orgType")
        source1 = orgResults1[index].get(u"source")
        orgId1 = orgResults1[index].get(u"orgIdOrCompanyId")
        money1 = orgResults1[index].get(u"money")
        if money1 != None and money1 !="":
            money1 = money1*1000000
        fund_associatio1 = orgResults1[index].get(u"fund_association")

        for viewOrgIndex in range(len(viewOrgResults2)):
            orgName2= viewOrgResults2[viewOrgIndex].get(u"orgName")
            if orgName2 == orgName1:
                short_name2= viewOrgResults2[viewOrgIndex].get(u"short_name")
                begin_time2 = viewOrgResults2[viewOrgIndex].get(u"begin_time")

                description2 = viewOrgResults2[viewOrgIndex].get(u"description")
                website2 = viewOrgResults2[viewOrgIndex].get(u"website")
                orgType2 = viewOrgResults2[viewOrgIndex].get(u"orgType")
                source2 = viewOrgResults2[viewOrgIndex].get(u"source")
                money2 = viewOrgResults2[viewOrgIndex].get(u"money")
                fund_association2 = viewOrgResults2[viewOrgIndex].get(u"fund_association")
                flag = (short_name1 == short_name2 and begin_time1 == begin_time2 and description1 == description2 and website1 == website2 and orgName1 == orgName2 and source1 == source2 and money1 == money2)
                # if flag == True:
                #     #管理基金中文简称
                #     manager_fund_short_name2 = viewOrgResults2[viewOrgIndex].get(u"manager_fund_short_name")
                #     #对比1.0和2.0库中投资机构的管理基金
                #     cpmpareCvs1and2OrgManageCompany(orgResults1[index], fields, orgId1, manager_fund_short_name2)
                if flag!=True:
                    if short_name1 != short_name2:
                         beizhuStr1 = u'cvs1和2中view_organization 机构简称 不完全匹配，此为cvs1中数据'
                         beizhuStr2 = u'cvs1和2中view_organization 机构简称 不完全匹配，此为cvs2中数据'
                         writeData(orgResults1[index], fields, beizhuStr1, viewOrgResults2[viewOrgIndex], beizhuStr2)
                         break
                    if fund_associatio1 != fund_association2:
                         beizhuStr1 = u'cvs1和2中view_organization 是否在基金业协会备案 不完全匹配，此为cvs1中数据'
                         beizhuStr2 = u'cvs1和2中view_organization 是否在基金业协会备案 不完全匹配，此为cvs2中数据'
                         writeData(orgResults1[index], fields, beizhuStr1, viewOrgResults2[viewOrgIndex], beizhuStr2)
                         break
                    if begin_time1 != begin_time2:
                         beizhuStr1 = u'cvs1和2中view_organization 成立时间 不完全匹配，此为cvs1中数据'
                         beizhuStr2 = u'cvs1和2中view_organization 成立时间 不完全匹配，此为cvs2中数据'
                         writeData(orgResults1[index], fields, beizhuStr1, viewOrgResults2[viewOrgIndex], beizhuStr2)
                         break
                    if description1 != description2:
                         beizhuStr1 = u'cvs1和2中view_organization 机构简介 不完全匹配，此为cvs1中数据'
                         beizhuStr2 = u'cvs1和2中view_organization 机构简介 不完全匹配，此为cvs2中数据'
                         writeData(orgResults1[index], fields, beizhuStr1, viewOrgResults2[viewOrgIndex], beizhuStr2)
                         break
                    if website1 != website2:
                         beizhuStr1 = u'cvs1和2中view_organization 机构网址 不完全匹配，此为cvs1中数据'
                         beizhuStr2 = u'cvs1和2中view_organization 机构网址 不完全匹配，此为cvs2中数据'
                         writeData(orgResults1[index], fields, beizhuStr1, viewOrgResults2[viewOrgIndex], beizhuStr2)
                         break
                    if source1 != source2:
                         beizhuStr1 = u'cvs1和2中view_organization 资本来源 不完全匹配，此为cvs1中数据'
                         beizhuStr2 = u'cvs1和2中view_organization 资本来源 不完全匹配，此为cvs2中数据'
                         writeData(orgResults1[index], fields, beizhuStr1, viewOrgResults2[viewOrgIndex], beizhuStr2)
                         break
                    if money1 != money2:
                         beizhuStr1 = u'cvs1和2中view_organization 管理资金总额 不完全匹配，此为cvs1中数据'
                         beizhuStr2 = u'cvs1和2中view_organization 管理资金总额  不完全匹配，此为cvs2中数据'
                         writeData(orgResults1[index], fields, beizhuStr1, viewOrgResults2[viewOrgIndex], beizhuStr2)
                         break
                else:
                    break

                #2.0中注册地址和联系地址
                address2 = viewOrgResults2[viewOrgIndex].get(u"address")
                #2.0库中联系电话
                telephone2 = viewOrgResults2[viewOrgIndex].get(u"telephone")

                #判断‘详细地址’ 和 ‘联系电话’ 是否一致
                isAddressBreak = compareAddress(orgResults1[index], fields, regAddress1, viewOrgResults2[viewOrgIndex], address2,orgId1)
                if isAddressBreak == "Addressbreak":
                    break

                #判断‘联系电话’ 是否一致  '2'代表判断联系电话
                isTelephoneBreak =compareCvs1and2OrgContact(orgResults1[index], fields, orgId1, telephone2, viewOrgResults2[viewOrgIndex],'2')
                if isTelephoneBreak == "telephoneBreak":
                    break

                #判断‘策略描述’ 是否一致
                strategyDescribe2 = viewOrgResults2[viewOrgIndex].get(u"strategyDescribe")
                isStrategyDescribeBreak = compareCvs1and2OrgStrategyCnDesc(orgResults1[index], fields, orgId1, strategyDescribe2, viewOrgResults2[viewOrgIndex])
                if isStrategyDescribeBreak == "strategyDescribebreak":
                    break

            else:
                continue

    db.close()

def compareCvs1and2OrgContact(orgResults1, fields, orgId1, contact_address_data2, viewOrgResults2,type):
    # db = MySQLdb.connect("119.254.13.183", "amy_zhang", "ZHE0yeuWHAkqyUdw", "datacenter_online_20180609", charset='utf8',
    #                      cursorclass=MySQLdb.cursors.DictCursor)
    db = MySQLdb.connect("39.106.211.74", "read", "aaKcKA6iDXyJVeF6", "datacenter_online", charset='utf8',
                         cursorclass=MySQLdb.cursors.DictCursor)
    cursor = db.cursor()

    sql = "SELECT b.cn_address, b.telephone " \
          "FROM dc_organization a, dc_contact b " \
          "WHERE b.context_type=2 AND a.id = b.context_id AND a.`status`=1 AND a.id='%s'" % orgId1

    cursor.execute(sql)
    orgContactResults1 = cursor.fetchall()
    for index in range(len(orgContactResults1)):
        #type == 1 为判断详细地址，type==2 为判断联系电话
        if type == '1':
            contactAddress1 = orgContactResults1[index].get(u"cn_address")
            contact_address_data2 = viewOrgResults2.get(u"address")
            #如果1.0库中联系地址有数据，判断2.0库中是否包含此联系地址
            if contactAddress1!= None:
                #如果1.0中联系地址有数据，但是2.0库中的注册和联系地址为空 则直接记录数据问题
                if contact_address_data2 == None or contact_address_data2 == "[]":
                    contact_address_data2 = "None"
                    beizhuStr1=u'cvs2中此投资机构缺失此条联系地址，此为cvs1中联系地址：' + contactAddress1
                    beizhuStr2=u'cvs2中此投资机构缺失此条联系地址，此为cvs2中联系地址：' + contact_address_data2
                    writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2)
                    return "Addressbreak"
                else:
                    #判断2.0库中是否包含1.0库中的联系地址
                    isContactAddress = contactAddress1 in contact_address_data2
                    if isContactAddress == False:
                        beizhuStr1=u'cvs2中此投资机构缺失此条联系地址，此为cvs1中联系地址：' + contactAddress1
                        beizhuStr2=u'cvs2中此投资机构缺失此条联系地址，此为cvs2中联系地址：' + contact_address_data2
                        writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2)
                        return "Addressbreak"
            #else如果1.0库中联系地址无数据，则看1.0中company表是否有注册地址信息，如果有注册地址信息，则2.0库中应该显示注册地址信息
            else:
                return "contactAddressNull"
        elif type == '2':
            telephone1 = orgContactResults1[index].get(u"telephone")
            telephone2 = viewOrgResults2.get(u"telephone")
            if telephone1!= None and telephone1 !="":
                #如果1.0中联系电话有数据，但是2.0库中的联系电话为空 则直接记录数据问题
                if telephone2 == None or telephone2 == "[]":
                    telephone2 = "None"
                    beizhuStr1=u'cvs2中此投资机构缺失此条联系电话，此为cvs1中联系电话：' + telephone1
                    beizhuStr2=u'cvs2中此投资机构缺失此条联系电话，此为cvs2中联系电话：' + telephone2
                    writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2)
                    return "telephoneBreak"
                else:
                    #如果1.0中联系电话有数据，同时2.0库中的联系电话不为空，判断2.0库中是否包含1.0库中的联系电话
                    isContactTelephone = telephone1 in telephone2
                    if isContactTelephone == False:
                        beizhuStr1=u'cvs2中此投资机构缺失此条联系电话，此为cvs1中联系电话：' + telephone1
                        beizhuStr2=u'cvs2中此投资机构缺失此条联系电话，此为cvs2中联系电话：' + telephone2
                        writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2)
                        # writeContactTelephoneData(orgResults1, fields, telephone1, viewOrgResults2, telephone2)
                        return "telephoneBreak"



        # if len(orgContactResults1) == 0:
        #     orgResults1.update(beizhu=u'cvs2中此投资机构管理基金缺失此条管理基金，此为cvs1中数据：' + ManageCompanyCnName1)
        #     for index in range(len(orgContactResults1)):
        #         ManageCompanyCnName1 = orgContactResults1[index].get(u"cn_name")
        #         flag = (ManageCompanyCnName1 != None and contact_address_data2 == None)
        #         if flag:
        #             orgResults1.update(beizhu=u'cvs2中此投资机构管理基金缺失此条管理基金，此为cvs1中数据：' + ManageCompanyCnName1)
        #             writeExcel(orgResults1, fields)
        #         elif ManageCompanyCnName1 in contact_address_data2:
        #             continue
        #         else:
        #             orgResults1.update(beizhu=u'cvs2中此投资机构管理基金缺失此条管理基金，此为cvs1中数据：' + ManageCompanyCnName1)
        #             writeExcel(orgResults1, fields)

    db.close()

def compareAddress(orgResults1, fields, regAddress1, viewOrgResults2, address2,orgId1):
    #判断2.0库中是否包含1.0的联系地址
    isok = compareCvs1and2OrgContact(orgResults1, fields, orgId1, address2,viewOrgResults2,"1")
    #1.0库中联系地址有数据，但是2.0库中不包含此联系地址，则此条数据不符合条件，excel记录问题后直接跳出循环进入到下一条数据的判断。因为2.0库中view表地址取值为先取联系地址，联系地址无数据时才取注册地址，则联系地址有数据时必须先取联系地址数据
    #2.0库中不包含1.0中的联系地址
    if isok=="Addressbreak":
        return "Addressbreak"

    #如果1.0的联系地址为空，则继续判断1.0中注册地址
    elif isok == "contactAddressNull":
        if regAddress1 != None and regAddress1 !='':
            #如果1.0中有注册地址，2.0中 注册地址为空 则有问题直接记录问题数据
            if address2 == None or address2 == "[]":
                address2 = "None"
                beizhuStr1=u'cvs2中此投资机构缺失此条注册地址，此为cvs1中注册地址：' + regAddress1
                beizhuStr2=u'cvs2中此投资机构缺失此条注册地址，此为cvs2中注册地址：' + address2
                writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2)
                return "Addressbreak"
            else:
                #判断2.0库中是否包含1.0的注册地址,如果不包含，直接记录问题
                isRegAddress = regAddress1 in address2
                if isRegAddress != True:
                    beizhuStr1=u'cvs2中此投资机构缺失此条注册地址，此为cvs1中注册地址：' + regAddress1
                    beizhuStr2=u'cvs2中此投资机构缺失此条注册地址，此为cvs2中注册地址：' + address2
                    writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2)
                    return "Addressbreak"

    # if regAddress1 != None and regAddress1 !='':
    #     #如果1.0中有注册地址，2.0中 注册和联系地址为空 则有问题直接记录问题数据
    #     if address2 == None or address2 == "[]":
    #         writeRegAddressData(orgResults1,fields,regAddress1,viewOrgResults2,"None")
    #         return "Addressbreak"
    #     else:
    #         #判断2.0库中是否包含1.0的注册地址
    #         isRegAddress = regAddress1 in address2
    #         if isRegAddress == True:
    #             #判断2.0库中是否包含1.0的联系地址
    #             isok = compareCvs1and2OrgContact(orgResults1, fields, orgId1, address2,viewOrgResults2,"1")
    #             #2.0库中不包含1.0中的联系地址
    #             if isok=="Addressbreak":
    #                 return "Addressbreak"
    #         else:
    #             writeRegAddressData(orgResults1,fields,regAddress1,viewOrgResults2,address2)
    #             return "Addressbreak"
    # else:
    #     #如果1.0中有注册地址，2.0中 注册和联系地址为空 则有问题直接记录问题数据
    #     if address2 == None or address2 == "[]":
    #         #判断2.0库中是否包含1.0的联系地址
    #         isok = compareCvs1and2OrgContact(orgResults1, fields, orgId1, address2,viewOrgResults2,"1")
    #         #2.0库中不包含1.0中的联系地址
    #         if isok=="Addressbreak":
    #             return "Addressbreak"

def compareCvs1and2OrgStrategyCnDesc(orgResults1, fields, orgId1, strategyDescribe2, viewOrgResults2):
    # db = MySQLdb.connect("119.254.13.183", "amy_zhang", "ZHE0yeuWHAkqyUdw", "datacenter_online_20180609", charset='utf8',
    #                      cursorclass=MySQLdb.cursors.DictCursor)
    db = MySQLdb.connect("39.106.211.74", "read", "aaKcKA6iDXyJVeF6", "datacenter_online", charset='utf8',
                         cursorclass=MySQLdb.cursors.DictCursor)
    cursor = db.cursor()

    sql = "SELECT b.strategy_cn_desc AS strategyDescribe " \
          "FROM dc_organization a, dc_organization_strategy b " \
          "WHERE a.id = b.organization_id AND a.`status`=1 AND b.strategy_cn_desc IS NOT NULL " \
          "AND b.strategy_cn_desc !='' AND a.id='%s'" % orgId1

    cursor.execute(sql)
    orgStrategyDescResults1 = cursor.fetchall()
    for index in range(len(orgStrategyDescResults1)):
        strategyDescribe1 = orgStrategyDescResults1[index].get(u"strategyDescribe")
        # strategyDescribe2 = viewOrgResults2.get(u"address")
        if strategyDescribe1!= None:
            #如果1.0中策略描述有数据，但是2.0库中的策略描述为空 则直接记录数据问题
            if strategyDescribe2 == None or strategyDescribe2 == "":
                #填充excel列表中策略描述的值，即searchCvs1OrgList方法中sql策略描述的值
                # orgstrategyDescribe1 = orgResults1.get(u"strategyDescribe")
                # orgstrategyDescribe1 = strategyDescribe1

                strategyDescribe2 = "None"
                beizhuStr1=u'cvs2中此投资机构策略描述与cvs1中不匹配，此为cvs1中策略描述：' + strategyDescribe1
                beizhuStr2=u'cvs2中此投资机构策略描述与cvs1中不匹配，此为cvs1中策略描述：' + strategyDescribe2
                writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2)
                # writeStrategyDescribeData(orgResults1, fields, strategyDescribe1, viewOrgResults2, "None")
                return "strategyDescribebreak"
            else:
                #判断2.0库中是否包含1.0库中的策略描述
                isStrategyDescribe = strategyDescribe1 in strategyDescribe2
                if isStrategyDescribe == False:
                    # orgstrategyDescribe1 = orgResults1.get(u"strategyDescribe")
                    # orgstrategyDescribe1 = strategyDescribe1

                    beizhuStr1=u'cvs2中此投资机构策略描述与cvs1中不匹配，此为cvs1中策略描述：' + strategyDescribe1
                    beizhuStr2=u'cvs2中此投资机构策略描述与cvs1中不匹配，此为cvs1中策略描述：' + strategyDescribe2
                    writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2)
                    # writeStrategyDescribeData(orgResults1, fields, strategyDescribe1, viewOrgResults2, strategyDescribe2)
                    return "strategyDescribebreak"

    db.close()

def writeData(orgResults1, fields, beizhuStr1, viewOrgResults2, beizhuStr2):
     orgResults1.update(beizhu=beizhuStr1)
     writeExcel(orgResults1, fields)
     viewOrgResults2.update(beizhu=beizhuStr2)
     writeExcel(viewOrgResults2,fields)

# def writeRegAddressData(orgResults1, fields, regAddress1, viewOrgResults2, address2):
#
#     orgResults1.update(beizhu=u'cvs2中此投资机构缺失此条注册地址，此为cvs1中注册地址：' + regAddress1)
#     writeExcel(orgResults1, fields)
#     viewOrgResults2.update(beizhu=u'cvs2中此投资机构缺失此条注册地址，此为cvs2中注册地址：' + address2)
#     writeExcel(viewOrgResults2, fields)

# def writeContactAddressData(orgResults1, fields, contactAddress1, viewOrgResults2, contact_address_data2):
#     orgResults1.update(beizhu=u'cvs2中此投资机构缺失此条联系地址，此为cvs1中联系地址：' + contactAddress1)
#     writeExcel(orgResults1, fields)
#     viewOrgResults2.update(beizhu=u'cvs2中此投资机构缺失此条联系地址，此为cvs2中联系地址：' + contact_address_data2)
#     writeExcel(viewOrgResults2, fields)

# def writeContactTelephoneData(orgResults1, fields, telephone1, viewOrgResults2, telephone2):
#     orgResults1.update(beizhu=u'cvs2中此投资机构缺失此条联系电话，此为cvs1中联系电话：' + telephone1)
#     writeExcel(orgResults1, fields)
#     viewOrgResults2.update(beizhu=u'cvs2中此投资机构缺失此条联系电话，此为cvs2中联系电话：' + telephone2)
#     writeExcel(viewOrgResults2, fields)

# def writeStrategyDescribeData(orgResults1, fields, strategyDescribe1, viewOrgResults2, strategyDescribe2):
#     # orgResults1.update(strategyDescribe= strategyDescribe1)
#     orgResults1.update(beizhu=u'cvs2中此投资机构策略描述与cvs1中不匹配，此为cvs1中策略描述：' + strategyDescribe1)
#     writeExcel(orgResults1, fields)
#     # viewOrgResults2.update(strategyDescribe= strategyDescribe2)
#     viewOrgResults2.update(beizhu=u'cvs2中此投资机构策略描述与cvs1中不匹配，此为cvs1中策略描述：' + strategyDescribe2)
#     writeExcel(viewOrgResults2, fields)

def writeExcel(results,fields):

    fileName = 'dataViewOrganization.xlsx'
    p1 = os.path.exists(fileName)

    if p1:
        f = xlrd.open_workbook(fileName, formatting_info=True)
        sheet = f.sheet_by_index(0)
        rowNum = sheet.nrows
        newbook =copy(f)
        newsheet = newbook.get_sheet(0)

        num =0
        for j,q in enumerate(results):
            x = results["%s"%fields[num][0]]
            num = num+1
            # print rowNum,j,x
            newsheet.write(rowNum,j,x)

        newbook.save(fileName)

    else:

        workbook  = Workbook(encoding = 'utf-8')
        table = workbook .add_sheet('data')

        for field in range(0,len(fields)):
            table.write(0,field,fields[field][0])

        num =0
        for j,q in enumerate(results):
            x = results["%s"%fields[num][0]]
            num = num+1
            print 1,j,x
            table.write(1,j,x)

        workbook .save(fileName)
